package com.tsll.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tsll.dao.StaffMapper;
import com.tsll.dao.StafftransferMapper;
import com.tsll.entity.Staff;
import com.tsll.entity.Stafftransfer;
import com.tsll.service.StafftransferService;
import com.tsll.vo.RejectionTransferVo;
import com.tsll.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;
@Service
public class StafftransferServiceImp implements StafftransferService {
    @Autowired
    StafftransferMapper stafftransferMapper;
    @Autowired
    StaffMapper staffMapper;
    @Autowired
    HttpSession session;
    @Override
    @Transactional
    public int insertStaffTransfer(Staff stafftransfer) {
        staffMapper.updateById(stafftransfer.getStaffid(),3,null);
        return stafftransferMapper.insertStaffTransfer(stafftransfer) ;
    }
    @Override
    public Result checktransfer(Integer current, Integer pageSize) {
        //启动分页
        PageHelper.startPage(current, pageSize);
        List<Stafftransfer> list = stafftransferMapper.checktransfer();
        //封装到PgeInfo
        PageInfo<Stafftransfer> pageInfo = new PageInfo<Stafftransfer>(list);
        return new Result(200, "查询员工信息成功", pageInfo);

    }

    @Override
    public Result transfer(Integer current, Integer pageSize) {
        PageHelper.startPage(current, pageSize);
        List<Stafftransfer> list = stafftransferMapper.transfer();
        //封装到PgeInfo
        PageInfo<Stafftransfer> pageInfo = new PageInfo<Stafftransfer>(list);
        return new Result(200, "查询员工信息成功", pageInfo);
    }

    @Override
    @Transactional
    public Result adopt(Integer id,Integer staffid,Integer newdeptid) {
        Staff staff = (Staff) session.getAttribute("staff");
        stafftransferMapper.adopt(id,staff.getId());
        staffMapper.updateById(staffid,1,newdeptid);
        return new Result(200, "调动成功",null);
    }

    /**
     * 驳回员工申请
     * @param rejectionTransferVo
     * @return
     */
    @Override
    @Transactional
    public Result reject(RejectionTransferVo rejectionTransferVo) {
        Staff staff = (Staff) session.getAttribute("staff");
        rejectionTransferVo.setAuditorId(staff.getId());
        stafftransferMapper.reject(rejectionTransferVo);
        staffMapper.updateById(rejectionTransferVo.getStaffid(),1,null);
        return new Result(200, "驳回成功",null);
    }


    /**
     * 根据id删除信息
     * @param id
     * @return
     */
    @Override
    public Result deleteByPrimaryKey(Integer id) {
        stafftransferMapper.deleteByPrimaryKey(id);
        return new Result(200,"删除成功",null);
    }
}
